/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package lights.dracots;

import lights.interfaces.IField;
import lights.interfaces.IValuedField;

/**
 * @author Xing Jiankuan
 *
 */
public class ClassNameField extends Field {

	private static final long serialVersionUID = 7266062801536232511L;
	
	private boolean isMatchAll = false;

	public ClassNameField() {
		this(false);
	}
	
	public ClassNameField(boolean isMatchAll) {
		setMatchAll(isMatchAll);
	}
	
	/**
	 * @return the isMatchAll
	 */
	public boolean isMatchAll() {
		return isMatchAll;
	}

	/**
	 * @param isMatchAll the isMatchAll to set
	 */
	public void setMatchAll(boolean isMatchAll) {
		this.isMatchAll = isMatchAll;
	}

	/**
	 * This operator matches logic is described below:<br>
	 * <ul>
	 * <li>Pre-Condition: Two fields are string, and valid qualified class name 
	 * (in actual case). Otherwise, false is return.</li>
	 * <li>Each of following conditions makes match return true:
	 * <ul>
	 * <li>If both f1 and f2 are actuals, the Class object indicated by f2 is either same as, or the
	 * super class of the one by f1.</li>
	 * <li>If f2 is formal and f1 is actual or vice versa(match any class).</li>
	 * <li>If both f1 and f2 are formal.</li>
	 * </ul>
	 * </li>
	 * </ul>
	 * @author Xing Jiankuan
	 *
	 */
	@Override
	public boolean matches(IField field) {
		if (!isMatchAll)
			return super.matches(field);
		
		//perform match all
		int s1 = this.isFormal()?0:1;
		int s2 = field.isFormal()?2:4;
		
		switch (s1 + s2) {
		case 2:
			//both f1 and f2 are formal
			return isBothFieldsString(this, field);
		
		case 3:
			//f1 is actual and f2 is formal
			if (!isBothFieldsString(this, field))
				return false;
			try {
				Class.forName((String) ((IValuedField)this).getValue());
			} catch (ClassNotFoundException e){
				return false;
			}
			return true;
		case 4:
			//f1 is formal and f2 is actual
			if (!isBothFieldsString(this, field))
				return false;
			try {
				Class.forName((String) ((IValuedField)field).getValue());
			} catch (ClassNotFoundException e){
				return false;
			}
			return true;
		
		case 5:
			//both f1 and f2 are actual
			if (!isBothFieldsString(this, field))
				return false;
			try {
				Class clz1 = Class.forName((String) ((IValuedField)this).getValue());
				Class clz2 = Class.forName((String) ((IValuedField)field).getValue());
				return clz1.isAssignableFrom(clz2) || clz2.isAssignableFrom(clz1);
			} catch (ClassNotFoundException e){
				return false;
			}
		}
		
		return false;
	}
	
	private boolean isBothFieldsString(IField f1, IField f2) {
		return f1.getType().equals(String.class) && 
			f2.getType().equals(String.class);
	}
}
